
package com.usersmodule.servlet;
import com.usersmodule.bean.TbUserRoleMiddle;
import com.usersmodule.dao.TbUserRoleMiddleDAO;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import util.*;

public class TbUserRoleMiddleServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        if("add".equals(method)){
        int userId=Integer.parseInt(request.getParameter("userId"));
        int roleId=Integer.parseInt(request.getParameter("roleId"));
            if(new TbUserRoleMiddleDAO().add(new TbUserRoleMiddle(userId,roleId))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
            goList(request, response);
        }
        if("del".equals(method)){
            if(UtilDAO.delin("TbUserRoleMiddle", "UserRoleMiddleId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
            goList(request, response);
        }
        if("edit".equals(method)){
        int userRoleMiddleId=Integer.parseInt(request.getParameter("userRoleMiddleId"));
        int userId=Integer.parseInt(request.getParameter("userId"));
        int roleId=Integer.parseInt(request.getParameter("roleId"));
            if(new TbUserRoleMiddleDAO().edit(new TbUserRoleMiddle(userRoleMiddleId,userId,roleId))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
            goList(request, response);
        }
        if ("list".equals(method)) goList(request, response);
        if ("listById".equals(method)) {
            request.getSession(true).setAttribute("tbUserRoleMiddle",new TbUserRoleMiddleDAO().getTbUserRoleMiddleByUserRoleMiddleId(Integer.parseInt(request.getParameter("userRoleMiddleId"))));
            response.sendRedirect("UsersModule/TbUserRoleMiddleOperate.jsp");
    	}
        if("showadd".equals(method)){
            request.getSession().removeAttribute("UsersModule");
            response.sendRedirect("UsersModule/TbUserRoleMiddleOperate.jsp");
        }
    }
    public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.getSession().setAttribute("tbUserRoleMiddleList",new TbUserRoleMiddleDAO().getTbUserRoleMiddleAll());
        response.sendRedirect("UsersModule/TbUserRoleMiddleList.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
}
